package core.proto;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

class IconCellRenderer extends DefaultTreeCellRenderer {
	private static final long serialVersionUID = 1L;
	
	protected Color m_textSelectionColor;
	protected Color m_textNonSelectionColor;
	protected Color m_bkSelectionColor;
	protected Color m_bkNonSelectionColor;
	protected Color m_borderSelectionColor;

	protected boolean m_selected;

	public IconCellRenderer() {
		super();
		m_textSelectionColor = Color.black;//UIManager.getColor("Tree.selectionForeground");
		m_textNonSelectionColor = UIManager.getColor("Tree.textForeground");
		m_bkSelectionColor = Color.YELLOW; //UIManager.getColor("Tree.selectionBackground");
		m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground");
		m_borderSelectionColor = UIManager.getColor("Tree.selectionBorderColor");
		setOpaque(false);
	}

	public Component getTreeCellRendererComponent(JTree tree, 
			Object value, boolean isSelected, boolean expanded, boolean leaf, 
			int row, boolean hasFocus) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;//get the node to be rendered

		Color backgroundColor = m_textSelectionColor;

		// set tooltip
		setIconAndToolTip(node, tree);
		
		// set text
		Object obj = node.getUserObject();// the IconData object repres. the node
		setText(obj.toString());
		if (obj instanceof Boolean) setText("Retrieving data...");
		
		// set icon
		if (obj instanceof IconData) {
			IconData icon = (IconData)obj;//IconData object
			CaseNode casenode = (CaseNode) icon.getObject();
			this.setFont(icon.getFont());
			backgroundColor = icon.m_backgroudColor;
			this.setForeground(icon.getForeground());
			if (expanded){
				setIcon(icon.getExpandedIcon());
			} else {
				setIcon(icon.getIcon());
			}
			// set background
			if (icon.m_icon.equals(icon.m_expandedIcon) && node.isLeaf() && (FileTree.getSearchBar().isSearchStarted()!=true 
			        /**&& (FileTree.getSearchBar().searchField.getText() !=null || FileTree.getSearchBar().searchField.getText() ==""))*/
			        )){
			    icon.m_backgroudColor= Color.WHITE;
			    //iterate through openTabs to see if the file is one of the opened files
			    //if (FileTree.getSearchBar().isSearchStarted()!=true){
			   Iterator iterator = AllComponents.openTabs.iterator();
			    while (iterator.hasNext()){
			        if (casenode.getAbsolutePath().equals(iterator.next())){
			            icon.m_backgroudColor = FileTree.openFileNodeColor;
			            if (casenode.getAbsolutePath().equals(AllComponents.getPreviousPath())) {
		                    icon.m_backgroudColor = FileTree.openFileNodeColor;
		                }
		                if (casenode.getAbsolutePath().equals(AllComponents.getCurrentPath())) {
		                    icon.m_backgroudColor = FileTree.openFileNodeFocusedColor;
		                }
		                //}
			        }
			    }
			}
			else if (icon.m_icon.equals(icon.m_expandedIcon) && node.isLeaf() && 
			        FileTree.getSearchBar().searchField.getText().length()>0){
                if (node.getUserObject().toString().toLowerCase().indexOf(FileTree.getSearchBar().searchField.getText().toLowerCase()) != -1) {               
                    icon.m_backgroudColor = FileTree.SEARCHED_NODE_BACKGROUND_COLOR;
//                System.out.println(">>>>>>>>>>>>data.m_backgroudColor = Color.YELLOW");
                    //nodeList.add(node);
                } else {
                    icon.m_backgroudColor = Color.WHITE;
                }
			}
		} else {
			setIcon(null);
		}
		tree.repaint();
		// set font
		setFont(tree.getFont());
		
		// set foreground color
		setForeground(isSelected ? m_textSelectionColor : m_textNonSelectionColor);

		// set background color
		if (isSelected) {
        	setBackground(m_bkSelectionColor);
		} else {
			setBackground(backgroundColor);			
		}
		m_selected = isSelected;
    	
		return this;
	}
    
	public void paintComponent(Graphics g) 
	{
		Color bColor = getBackground();
		Icon icon = getIcon();

		g.setColor(bColor);
		int offset = 0;
		if(icon != null && getText() != null) 
			offset = (icon.getIconWidth() + getIconTextGap());
		g.fillRect(offset, 0, getWidth() - 1 - offset,
		getHeight() - 1);
		
		if (m_selected)  {
			g.setColor(m_borderSelectionColor);
			g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
		}
		super.paintComponent(g);
	}
  
    private void setIconAndToolTip(DefaultMutableTreeNode node, JTree tree) {
    	Object obj = node.getUserObject();
    	IconData icon = (IconData) obj;
    	setIcon(icon.m_icon);
    	
    	String text = ""; /**"Location: ";*/
//    	TreeNode[] nodeArray = node.getPath();
//    	if (nodeArray != null) {
//	    	for (TreeNode treeNode : nodeArray) {
//	    	    //System.out.println("this is line 37 CaseTreeCellRenderer");
//	    	    //System.out.println(treeNode.toString());
//	    		text = treeNode.toString();
//	    	}
//    	}
		text = node.toString();
    	setToolTipText(text);
    }
}